package com.api.util.repo.impl;

import com.api.test.Api;
import com.api.util.repo.DtoAccessor;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.DtoProperties;

public class ZyncroRepo {

	private final DtoAccessor dtoAccessor;
	private final Api serviceApi;
	private final Api oauthApi;

	public ZyncroRepo(String propertiesFile, Api serviceApi, Api oauthApi) {
		this.serviceApi = serviceApi;
		this.oauthApi = oauthApi;

		DtoProperties dtoRepository = new DtoProperties(propertiesFile);
		this.dtoAccessor = new DtoAccessor(dtoRepository);
	}

	public Domain getDomain(String domainName) {
		DtoFactory<Domain> factory = new DomainDtoFactory(serviceApi, dtoAccessor.getDtoRepository(), domainName);
		return dtoAccessor.getDto(domainName, factory);
	}

	public DomainUser getMainUser(String domainName) {
		this.getDomain(domainName);
		return dtoAccessor.getDto(domainName, DomainUser.class);
	}

	public DomainUser getDomainUser(String domainName, String permission) {
		DomainUser mainAdmin = this.getMainUser(domainName);
		DtoFactory<DomainUser> factory = new DomainUserDtoFactory(oauthApi, mainAdmin, domainName, permission);
		String property = String.format("%s-%s", domainName, permission);
		return dtoAccessor.getDto(property, factory);
	}

	public <T> T getPropertyDto(String property, DtoFactory<T> factory) {
		return dtoAccessor.getDto(property, factory);
	}
}
